<template>
  <div class="knowledge-file-list">
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="sysAttachment" label="缩略图" width="100">
        <template slot-scope="scope">
          <div class="img-box">
            <img class="thumbnail" :src="scope.row | fileThumbnail" />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="资源名称" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="size" label="资源大小">
        <template slot-scope="scope">
          {{ scope.row.size | fileSizeFilter }}
        </template>
      </el-table-column>
      <el-table-column prop="type" label="资源类型"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="240">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button type="success" size="mini" @click="handleLook(row)">
              预览
            </el-button>
            <el-button
              v-if="fileType.img.includes(row.type)"
              type="primary"
              size="mini"
              @click="setCoverImg(row)"
              :disabled="row.fileId == $store.getters.knowledgeFiledId"
            >
              设为封面图
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
    <Preview />
  </div>
</template>
<script>
import {
  sysKnowledgeFilesQuery,
  sysKnowledgeFilesDelete,
} from '@/api/admin/courseLibrary/sysKnowledgeFiles.js'
import { SetCoverImg } from '../../mixins/SetCoverImg'
import { FILETYPE } from '@/utils/config'
import Bus from '@/utils/bus.js'
export default {
  mixins: [SetCoverImg],
  components: {
    Preview: () => import('@/components/resource/preview.vue'),
  },
  data() {
    return {
      fileType: FILETYPE,
      listQuery: {
        page: 1,
        limit: 10,
        knowledgeId: this.$store.getters.knowledgeId,
      },
      tableData: [],
      pageObj: {},
    }
  },
  mounted() {
    this.getList()
    Bus.$on('refreshKnowledgeFilesList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshKnowledgeFilesList')
  },
  methods: {
    getList() {
      sysKnowledgeFilesQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.pageObj = res.data
      })
    },
    //预览
    handleLook(row) {
      Bus.$emit('showPreview', row)
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      sysKnowledgeFilesDelete(row.id).then((res) => {
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>
<style scoped>
.knowledge-file-list {
  padding-bottom: 20px;
}
.img-box {
  padding: 2px;
  width: auto;
  height: 40px;
  display: inline-block;
}
.thumbnail {
  width: auto;
  height: 100%;
  max-width: 60px;
}
</style>